package cn.schoolwow.workflow.module.task.action.flow.complete.nodeType.general;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowDefinition;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.task.action.flow.complete.step.businessType.HandleNormalBusinessNodeFlow;

import java.util.ArrayList;
import java.util.List;

public class HandleAuditNodeFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        handleNode(flowContext);
        setWorkFlowTaskResponse(flowContext);
    }

    @Override
    public String name() {
        return "处理人工审批节点";
    }

    private void handleNode(FlowContext flowContext){
        flowContext.executeFlowList(new HandleNormalBusinessNodeFlow());
    }

    private void setWorkFlowTaskResponse(FlowContext flowContext){
        WorkFlowDefinition workFlowDefinition = flowContext.checkInstanceData(WorkFlowDefinition.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        List<WorkFlowTask> workFlowTaskList = flowContext.getData("workFlowTaskList", List.class);

        List<WorkFlowTaskResponse> workFlowTaskResponseList = new ArrayList<>();
        for(WorkFlowTask workFlowTask:workFlowTaskList){
            WorkFlowTaskResponse workFlowTaskResponse = new WorkFlowTaskResponse();
            workFlowTaskResponse.instanceId = workFlowInstance.getId();
            workFlowTaskResponse.instanceStatus = workFlowInstance.getStatus();
            workFlowTaskResponse.definitionId = workFlowInstance.getDefinitionId();
            workFlowTaskResponse.definitionName = workFlowDefinition.getName();
            workFlowTaskResponse.taskId = workFlowTask.getId();
            workFlowTaskResponse.taskName = workFlowTask.getTaskName();
            workFlowTaskResponse.contextData = workFlowInstance.getContextData();
            workFlowTaskResponse.complete = false;
            workFlowTaskResponseList.add(workFlowTaskResponse);
        }
        flowContext.putTemporaryData("workFlowTaskResponseList", workFlowTaskResponseList);
    }
}
